<?
/************code by seven****************/
require("../../common/init.php");
require("author.php");
require("upfile.php");

$gee_cate=$product_category; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象
$gee_product = $product;
$gee_news = $news;
$gee_document = $document;

$do = '';
if (isset($_GET["do"]))$do = $_GET["do"];
$id=0; if (isset($_GET["id"])) $id = $_GET["id"];
$cate_id = 0; if(isset($_GET['cate_id'])) $cate_id = $_GET['cate_id'];

switch($do){
    case 'product_add':
        add_product();
        break;
    case 'product_cancel':
        delete();
        break;
    case 'tag_add':
        tag_add();
        break;
    default:
        product_list($cate_id);

}

function product_list($cate_id){
    global $gee_cate, $gee_product, $gee_news, $gee_document, $lang, $code_array, $h, $tag_list;
    $tag_type_name = $_GET['tag_type_name'];
    $tag_id = $_GET['tag_id'];
    $tag_type = $_GET['tag_type'];
    if($tag_type == 1){
        $list = $gee_product->get_rows(4096,'category_id', $cate_id);
    }else{
        if($cate_id == 1){
            $list = $gee_news->get_rows(4096);
        }else{
            $list = $gee_document->get_rows(4096);
        }
    }
    $tag_type = $_GET['tag_type'];
    //print_r($list);
    //echo $cate_id;

    $arr_cate=$code_array['tag_type'];
    $i = 1;
    $str = '<table width="100%">';
    $str .= '<tr>';
    foreach($list as $key => $val){
        $k = $i % 2;
        if($k == 1 && $i != 1) {
            $i = 1;
            $str .= '</tr><tr>';
        }
        $id = $val['id'];
//        $tag_list_product = $tag_list->get_row($id, 'p_id'
        $where = array();
        $where[]=array('name'=>'p_id','val'=>$id);
        $where[]=array('name'=>'tag_id','val'=>$tag_id);

    $tag_list_row = $tag_list->get_rows_advanced(1, $where);

        if($tag_list_row[0]['tag_id']){
            $str .= '<td style="width:50%; height:50px">'.$h->hidden('id', $id).'<a class="tag_cancel" href="javascript:void(0)" onclick="cate_cancel(this)"><span class="tag_list_name">'.decode($val['name_'.$lang]?$val['name_'.$lang]:$val['name']).'</span><span class="tag_list_operator">取消标签</span></a></td>';
        }else{
            $str .= '<td style="width:50%; height:50px">'.$h->hidden('id', $id).'<a class="tag_add" href="javascript:void(0)" onclick="cate_add(this)"><span class="tag_list_name">'.decode($val['name_'.$lang]?$val['name_'.$lang]:$val['name']).'</span><span class="tag_list_operator">添加标签</span></a></td>';
        }$i++;
    }
    while($i != 3){
        $str .= '<td style="width:50%"></td>';
        $i++;
    }
    $str .= '</tr>';
    $str .= '</table>';
    echo $str;
}// sql action / sql 动作

function add_product(){
    global $gee_product,$gee_news,$gee_document, $tags, $tag_list;
    $p_id = $_POST['p_id'];
    $tag_id = $_POST['tag_id'];
    $tag_type = $_POST['tag_type'];
    $cate_id = $_POST['cate_id'];

    if($tag_type == 1){
        $table_name = 'product';
    }else{
        if($cate_id == 1){
            $table_name = 'news';
        }else{
            $table_name = 'document';
        }
    }
    $arr=array(
        'tag_id' => $tag_id,
        'p_id'=>$p_id,
        'table_name' => $table_name,
    );

//    pre($arr); exit;
    $id = $tag_list->insert($arr);
    $count = $tags->get_row($tag_id);
    $tag_arr = array('count' => $count['count'] + 1);
    $tags->update($tag_arr, 'id', $tag_id);
    $arr['tag_list_name'] = $_POST['tag_list_name'];
    $arr['id'] = $id;
    echo json_encode($arr);
    // wheel("$url.php?promotion_id=".$promotion_id);
}

function delete()
{
    global $gee_product, $id, $tags, $tag_list;
    $p_id = $_POST['p_id'];
    $tag_id = $_POST['tag_id'];
    $where = array();
    $where[]=array('name'=>'p_id','val'=>$p_id);
    $where[]=array('name'=>'tag_id','val'=>$tag_id);
    $tag_list_row = $tag_list->get_rows_advanced(1, $where);
    $tag_list->delete($tag_list_row[0]['id']);

    $count = $tags->get_row($tag_id);
    $tag_arr = array('count' => $count['count'] - 1);
    $arr=array(
        'tag_id' => $tag_id,
        'p_id'=>$p_id,
    );
    $tags->update($tag_arr, 'id', $tag_id);
    $arr['tag_list_name'] = $_POST['tag_list_name'];
    $arr['id'] = $id;
    echo json_encode($arr);
    /*$arr=array(
        'discount_type'=>$discount_type,
        'promotion_id' => $promotion_id,
        'discount_value' => $discount_value,
    );
    //pre($arr); exit;
    $gee_product->update($arr, 'id', $id );
    $arr['discount_value'] = sprintf("%.2f", $arr['discount_value']);
    $arr['discount_type_name'] = $discount_type_name;
    echo json_encode($arr);
//echo $id;*/
}


function tag_add(){
    global $tags, $tag_list,$lang;
    $value = $_POST['value'];
    $arr=array(
        'name_'.$lang => $value,
        'user_id' => decrypt($_COOKIE['userid']),
        'is_filter' => 0,
        'is_index' => 0
    );
    if(!empty($value)){
        $gee_empty = $tags->get_row($value, 'name_'.$lang);
        if(!$gee_empty){
            if($tags->insert($arr)){
                $id = $tags->get_last_insert_id();
                $data_arr = array('id'=>$id,'value'=>$value);
            }
        }else{
            $data_arr = array('id'=>$gee_empty['id'], 'value'=>$value);
        }
    }else{
        $data_arr = array('id'=>0);
    }
    echo json_encode($data_arr);
}

// end article.php
